package biz.inv.mgbean;

import biz.common.dto.ReportCriteria;
import biz.common.service.ProdLocationSvr;
import biz.inv.dto.StockInDetail;
import biz.inv.service.ReportStockInSummarySvr;
import biz.master.dto.Branch;
import glen.util.BaseManageBean;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Test on 3/8/14.
 */
@ManagedBean(name = "reportStockInSummaryMgb")
@ViewScoped
public class ReportStockInSummaryMgb extends BaseManageBean<StockInDetail, ReportStockInSummarySvr> {

    private ReportCriteria criteria = new ReportCriteria();
    private ProdLocationSvr prodLocationSvr = new ProdLocationSvr();
    JasperPrint jasperPrint;


    public ReportStockInSummaryMgb() throws InstantiationException, IllegalAccessException {
        super(StockInDetail.class, ReportStockInSummarySvr.class);
    }

    public String exportPDF() {

        HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
        httpServletResponse.addHeader("Content-disposition", "attachment; filename=report.pdf");
        ServletOutputStream servletOutputStream;
        try {
            initReport();
            servletOutputStream = httpServletResponse.getOutputStream();
            JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);
        } catch (Exception e) {

            e.printStackTrace();
        }

        FacesContext.getCurrentInstance().responseComplete();
        return null;
    }

    public void initReport() throws JRException {
        JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(this.resultSearch);
        ServletContext servletContext = (ServletContext) FacesContext
                .getCurrentInstance().getExternalContext().getContext();
        InputStream is = servletContext.getResourceAsStream("/modules/report/StockInSummary.jasper");
        //String  reportPath=  FacesContext.getCurrentInstance().getExternalContext(.getRealPath("/modules/report/StockInSummary.jasper");
        jasperPrint = JasperFillManager.fillReport(is, new HashMap(), beanCollectionDataSource);
    }

    public ReportCriteria getCriteria() {
        return criteria;
    }

    public void setCriteria(ReportCriteria criteria) {
        this.criteria = criteria;
    }

    public void search() {
        resultSearch = svr.search(criteria);
    }

    public void reset() {
        resultSearch = null;
    }

    public List<Branch> getBranchList() {
        return prodLocationSvr.searchBranch(null);
    }
}
